package com.itheima.advantagesup.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.advantagesup.common.PageBean;
import com.itheima.advantagesup.dto.*;
import com.itheima.advantagesup.entity.SysUser;
import com.itheima.advantagesup.mapper.SysUserMapper;
import com.itheima.advantagesup.service.SysUserService;
import com.itheima.advantagesup.utils.IpUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.time.LocalDateTime;

/**
 * SysUserServiceImpl
 *
 * @author liliudong
 * @version 1.0
 * @description
 * @date 2023/7/26 14:58
 */
@Slf4j
@Service
@RequiredArgsConstructor
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private final SysUserMapper sysUserMapper;

    @Autowired
    private final IpUtils ipUtils;

    @Override
    public void create(SysUserCreateDTO sysUserCreateDTO) {
        SysUser sysUser = new SysUser();

        BeanUtils.copyProperties(sysUserCreateDTO,sysUser);

        String password = DigestUtils.md5DigestAsHex(sysUserCreateDTO.getPassword().getBytes());
        sysUser.setLoginIp(ipUtils.getClientIp());
        sysUser.setPassword(password);
        sysUser.setLoginDate(LocalDateTime.now());
        sysUser.setCreateDate(LocalDateTime.now());
        sysUser.setUpdateDate(LocalDateTime.now());
        sysUser.setDelFlag("0");

        sysUserMapper.insert(sysUser);
    }

    @Override
    public void deleteById(SysUserDelDTO sysUserDelDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserDelDTO,sysUser);
        sysUserMapper.deleteById(sysUser);
    }

    @Override
    public PageBean page(SysUserPageDTO sysUserPageDTO) {
        PageHelper.startPage(Integer.valueOf(sysUserPageDTO.getPage()),Integer.valueOf(sysUserPageDTO.getLimit()));

        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserPageDTO,sysUser);

        Page<SysUser> page = sysUserMapper.selectList(sysUser);
        log.info("查询到{}",page.getResult().toString());

        Integer count = sysUserMapper.selectCount();
        page.forEach(s ->{s.setPassword(null);});
        PageBean pageBean = new PageBean(count,(int)page.getTotal(),Integer.valueOf(sysUserPageDTO.getLimit()),1,page.getResult());
        return pageBean;
    }

    @Override
    public SysUser queryById(SysUserInfoDTO sysUserInfoDTO) {

        SysUser sysUser = sysUserMapper.selectById(sysUserInfoDTO.getId());
        return sysUser;
    }

    @Override
    public void updateById(SysUserUpdateDTO sysUserUpdateDTO) {
        SysUser sysUser = new SysUser();
        BeanUtils.copyProperties(sysUserUpdateDTO,sysUser);

        sysUser.setLoginIp(ipUtils.getClientIp());
        sysUser.setLoginDate(LocalDateTime.now());
        sysUser.setUpdateDate(LocalDateTime.now());

        sysUserMapper.updateById(sysUser);
    }

}
